﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;

namespace YJKArch.DictModule
{
	public partial class DlgDict : System.Windows.Forms.Form
	{
		public static DlgDict GetInstance(ExternalCommandData cd)
		{
			if (DlgDict.dlgMain == null || DlgDict.dlgMain.IsDisposed)
			{
				DlgDict.dlgMain = new DlgDict(cd);
			}
			else
			{
				DlgDict.dlgMain.InitTree();
			}
			return DlgDict.dlgMain;
		}

		public DlgDict(ExternalCommandData cd)
		{
			this.InitializeComponent();
			this.revit = cd;
			this.tempFile = Assist.FileUtil.GetTempDataFile();
			string text = Assembly.GetExecutingAssembly().Location;
			int length = text.LastIndexOf("\\");
			text = text.Substring(0, length);
			int length2 = text.LastIndexOf("\\");
			text = text.Substring(0, length2);
			text += "\\Data\\dict.xml";
			this.dictFile = new FileInfo(text);
			if (!this.dictFile.Exists)
			{
				MessageBox.Show("默认词库文件不存在。\n请检查软件是否安装正确，或手动选择词库文件。", "盈建科词库");
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.FileName = "Dict.xml";
				openFileDialog.Filter = "词库(*.xml)|*.xml";
				if (openFileDialog.ShowDialog() != DialogResult.OK)
				{
					return;
				}
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("词库文件名", openFileDialog.FileName);
				Assist.FileUtil.WriteTempData(this.tempFile.FullName, "专业词库", ConfigData.DictData, dictionary);
				this.dictFile = new FileInfo(openFileDialog.FileName);
			}
			string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempFile.FullName, "专业词库", "词库文件名");
			FileInfo fileInfo = null;
			try
			{
				fileInfo = new FileInfo(dataFromTempFile);
			}
			catch (Exception)
			{
			}
			if (fileInfo != null)
			{
				this.dictFile = fileInfo;
			}
			this.initRecently();
			this.InitTree();
			this.initStyle();
			this.initAlign();
		}

		private void initRecently()
		{
			try
			{
				new DictOperation(this.dictFile.FullName).FillCbx(this.comboBox1);
			}
			catch (Exception)
			{
			}
		}

		public void InitTree()
		{
			try
			{
				new DictOperation(this.dictFile.FullName).FillTree(this.tvDirectory);
			}
			catch (Exception)
			{
			}
		}

		private void initStyle()
		{
			try
			{
				this.cbxStyle.Items.Clear();
				string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempFile.FullName, "专业词库", "样式");
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.revit.Application.ActiveUIDocument.Document);
				filteredElementCollector.OfClass(typeof(TextNoteType));
				if (filteredElementCollector != null)
				{
					IEnumerable<Element> enumerable = filteredElementCollector.ToElements();
					new List<Element>();
					foreach (Element element in enumerable)
					{
						this.cbxStyle.Items.Add(element.Name);
					}
				}
				if (this.cbxStyle.Items.Count > 0)
				{
					this.cbxStyle.SelectedIndex = 0;
				}
				int num = 0;
				Graphics graphics = base.CreateGraphics();
				for (int i = 0; i < this.cbxStyle.Items.Count; i++)
				{
					this.cbxStyle.SelectedIndex = i;
					SizeF sizeF = graphics.MeasureString(this.cbxStyle.Text, this.cbxStyle.Font);
					if (num < (int)sizeF.Width)
					{
						num = (int)sizeF.Width;
					}
				}
				this.cbxStyle.DropDownWidth = this.cbxStyle.Width;
				if (this.cbxStyle.DropDownWidth < num)
				{
					this.cbxStyle.DropDownWidth = num;
				}
				foreach (object obj in this.cbxStyle.Items)
				{
					if (obj.ToString() == dataFromTempFile)
					{
						this.cbxStyle.SelectedItem = obj;
						break;
					}
					if (this.cbxStyle.Items.Count > 0)
					{
						this.cbxStyle.SelectedIndex = 0;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
			}
		}

		private void initAlign()
		{
			try
			{
				this.cbxHorizonAlign.Items.Clear();
				this.cbxHorizonAlign.Items.AddRange(new object[]
				{
					"左对齐",
					"居中对齐",
					"右对齐"
				});
				string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempFile.FullName, "专业词库", "对齐方式");
				foreach (object obj in this.cbxHorizonAlign.Items)
				{
					if (obj.ToString() == dataFromTempFile)
					{
						this.cbxHorizonAlign.SelectedItem = obj;
						break;
					}
					if (this.cbxHorizonAlign.Items.Count > 0)
					{
						this.cbxHorizonAlign.SelectedIndex = 0;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
			}
		}

		private void initEntry()
		{
			try
			{
				string dataFromTempFile = Assist.FileUtil.GetDataFromTempFile(this.tempFile.FullName, "专业词库", "词条名");
				bool flag = false;
				foreach (object obj in this.lbxEntry.Items)
				{
					if (obj.ToString() == dataFromTempFile)
					{
						this.lbxEntry.SelectedItem = obj;
						flag = true;
						this.lbxEntry.SelectedItem = obj;
						break;
					}
				}
				if (!flag && this.lbxEntry.Items.Count > 0)
				{
					this.lbxEntry.SelectedIndex = 0;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
			}
		}

		private void DglDict_Load(object sender, EventArgs e)
		{
		}

		private void lbxEntry_MouseEnter(object sender, EventArgs e)
		{
			this.lbxEntry.Focus();
		}

		public void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
		{
			this.lbxEntry.Items.Clear();
			this.richTextBox1.Text = "";
			if (e.Node.Text != "最近使用的词")
			{
				this.lbxEntry.Sorted = true;
			}
			else
			{
				this.lbxEntry.Sorted = false;
			}
			this.GetChildAttributes((XmlNode)e.Node.Tag, this.lbxEntry);
			this.btnPlace.Enabled = false;
			this.initEntry();
		}

		public void GetChildAttributes(XmlNode node, ListBox lbx)
		{
			foreach (object obj in node.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == "词条")
				{
					lbx.Items.Insert(0, new StringEntry(xmlNode.Attributes["name"].Value, (xmlNode.InnerText == "") ? xmlNode.Attributes["name"].Value : xmlNode.InnerText, xmlNode));
					this.GetChildAttributes(xmlNode, lbx);
				}
				else if (xmlNode.Name == "目录" && xmlNode.Attributes["name"].Value != "最近使用的词")
				{
					this.GetChildAttributes(xmlNode, lbx);
				}
			}
		}

		public void GetChildAttributes(XmlNode node, List<StringEntry> seList)
		{
			foreach (object obj in node.ChildNodes)
			{
				XmlNode xmlNode = (XmlNode)obj;
				if (xmlNode.Name == "词条")
				{
					seList.Add(new StringEntry(xmlNode.Attributes["name"].Value, (xmlNode.InnerText == "") ? xmlNode.Attributes["name"].Value : xmlNode.InnerText, xmlNode));
					this.GetChildAttributes(xmlNode, seList);
				}
				else if (xmlNode.Name == "目录" && xmlNode.Attributes["name"].Value != "最近使用的词")
				{
					this.GetChildAttributes(xmlNode, seList);
				}
			}
		}

		private int GetChildCount(XmlNode node)
		{
			int num = 0;
			IEnumerator enumerator = node.ChildNodes.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((XmlNode)enumerator.Current).Name == "词条")
					{
						num++;
					}
				}
			}
			return num;
		}

		private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lbxEntry.SelectedItem != null)
			{
				StringEntry stringEntry = (StringEntry)this.lbxEntry.SelectedItem;
				this.richTextBox1.Text = stringEntry.Text;
				this.btnPlace.Enabled = true;
			}
		}

		private void lbxEntry_DoubleClick(object sender, EventArgs e)
		{
			if (this.lbxEntry.SelectedItem != null)
			{
				XmlNode xmlNode = (XmlNode)this.tvDirectory.Nodes[0].Nodes[0].Tag;
				DictOperation.AddStringEntry(xmlNode, new StringEntry(this.lbxEntry.SelectedItem.ToString(), this.richTextBox1.Text, null), this.dictFile.FullName);
				while (this.GetChildCount(xmlNode) > 15)
				{
					DictOperation.RemoveFirstStringEntry(xmlNode, this.dictFile.FullName);
				}
				string text = this.cbxHorizonAlign.Text;
				if (!(text == "左对齐"))
				{
					if (!(text == "居中对齐"))
					{
						if (text == "右对齐")
						{
                            this.alignFlags = (TextAlignFlags)256;
						}
					}
					else
					{
                        this.alignFlags = (TextAlignFlags)128;
					}
				}
				else
				{
                    this.alignFlags = (TextAlignFlags)64;
				}
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("词条名", this.lbxEntry.Text);
				dictionary.Add("样式", this.cbxStyle.Text);
				dictionary.Add("对齐方式", this.cbxHorizonAlign.Text);
				Assist.FileUtil.WriteTempData(this.tempFile.FullName, "专业词库", ConfigData.DictData, dictionary);
				base.DialogResult = DialogResult.OK;
				base.Close();
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "A");
		}

		private void lbxEntry_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				int num = this.lbxEntry.IndexFromPoint(new System.Drawing.Point(e.X, e.Y));
				if (num >= 0 && num < this.lbxEntry.Items.Count)
				{
					this.lbxEntry.SelectedIndex = num;
				}
			}
		}

		private void tvDirectory_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Right)
			{
				TreeNode nodeAt = this.tvDirectory.GetNodeAt(e.X, e.Y);
				if (nodeAt != null)
				{
					this.tvDirectory.SelectedNode = nodeAt;
				}
			}
		}

		private void Search(ListBox lbx, string str)
		{
			int length = str.Trim().Length;
			for (int i = 0; i < lbx.Items.Count; i++)
			{
				StringEntry stringEntry = (StringEntry)lbx.Items[i];
				bool flag = false;
				if (stringEntry.GetThumbnail().Length >= length)
				{
					for (int j = 0; j < length; j++)
					{
						if (!stringEntry.GetThumbnail().Substring(j, 1).Equals(str.Substring(j, 1)) && !stringEntry.Name.Substring(j, 1).Equals(str.Substring(j, 1)))
						{
							flag = false;
							break;
						}
						flag = true;
					}
				}
				if (flag)
				{
					lbx.SelectedIndex = i;
					return;
				}
			}
		}

		private void button1_Click_1(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "B");
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "C");
		}

		private void button5_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "D");
		}

		private void button4_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "E");
		}

		private void button3_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "F");
		}

		private void button8_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "G");
		}

		private void button7_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "H");
		}

		private void button6_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "I");
		}

		private void button11_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "J");
		}

		private void button10_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "K");
		}

		private void button9_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "L");
		}

		private void button14_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "M");
		}

		private void button13_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "N");
		}

		private void button12_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "O");
		}

		private void button17_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "P");
		}

		private void button16_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "Q");
		}

		private void button15_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "R");
		}

		private void button21_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "S");
		}

		private void button20_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "T");
		}

		private void button19_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "W");
		}

		private void button18_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "X");
		}

		private void button23_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "Y");
		}

		private void button22_Click(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, "Z");
		}

		private void comboBox1_TextChanged(object sender, EventArgs e)
		{
			this.Search(this.lbxEntry, this.comboBox1.Text.ToUpper());
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
		}

		private void button24_Click(object sender, EventArgs e)
		{
		}

		private void groupBox1_Enter(object sender, EventArgs e)
		{
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
		}

		private void btnChose_Click(object sender, EventArgs e)
		{
		}

		private void btnOutput_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.FileName = "Dict.xml";
			saveFileDialog.Filter = "词库(*.xml)|*.xml";
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				try
				{
					this.dictFile.CopyTo(saveFileDialog.FileName, true);
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message, "盈建科提示");
				}
			}
		}

		private void btnInput_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = "词库(*.xml)|*.xml";
			if (openFileDialog.ShowDialog() == DialogResult.OK && MessageBox.Show("是否替换词库文件？", "盈建科提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
			{
				try
				{
					if (!this.IsStandardDictFile(openFileDialog.FileName))
					{
						MessageBox.Show("不是正确的词库文件！无法替换。", "盈建科提示");
						return;
					}
					this.tempFile = Assist.FileUtil.GetTempDataFile();
					Dictionary<string, string> dictionary = new Dictionary<string, string>();
					dictionary.Add("词库文件名", openFileDialog.FileName);
					Assist.FileUtil.WriteTempData(this.tempFile.FullName, "专业词库", ConfigData.DictData, dictionary);
					this.dictFile = new FileInfo(openFileDialog.FileName);
					DictOperation dictOperation = new DictOperation(this.dictFile.FullName);
					this.lbxEntry.Items.Clear();
					dictOperation.FillCbx(this.comboBox1);
					this.comboBox1.ContextMenuStrip = this.cmsEntry;
					dictOperation.FillTree(this.tvDirectory);
					this.initStyle();
					this.initAlign();
				}
				catch (Exception)
				{
					MessageBox.Show("请选择正确的词库文件！", "盈建科提示");
				}
			}
			this.initRecently();
			this.InitTree();
			this.initStyle();
			this.initAlign();
		}

		private bool IsStandardDictFile(string filleName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(filleName);
			}
			catch (Exception)
			{
				return false;
			}
			return xmlDocument.SelectSingleNode("盈建科词库") != null;
		}

		private void btnPlace_Click(object sender, EventArgs e)
		{
			if (this.lbxEntry.SelectedItem != null)
			{
				XmlNode xmlNode = (XmlNode)this.tvDirectory.Nodes[0].Nodes[0].Tag;
				DictOperation.AddStringEntry(xmlNode, new StringEntry(this.lbxEntry.SelectedItem.ToString(), this.richTextBox1.Text, null), this.dictFile.FullName);
				while (this.GetChildCount(xmlNode) > 15)
				{
					DictOperation.RemoveFirstStringEntry(xmlNode, this.dictFile.FullName);
				}
				string text = this.cbxHorizonAlign.Text;
				if (!(text == "左对齐"))
				{
					if (!(text == "居中对齐"))
					{
						if (text == "右对齐")
						{
                            this.alignFlags = (TextAlignFlags)256;
						}
					}
					else
					{
                        this.alignFlags = (TextAlignFlags)128;
					}
				}
				else
				{
                    this.alignFlags = (TextAlignFlags)64;
				}
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				dictionary.Add("词条名", this.lbxEntry.Text);
				dictionary.Add("样式", this.cbxStyle.Text);
				dictionary.Add("对齐方式", this.cbxHorizonAlign.Text);
				Assist.FileUtil.WriteTempData(this.tempFile.FullName, "专业词库", ConfigData.DictData, dictionary);
			}
		}

		private void btnReplace_Click(object sender, EventArgs e)
		{
		}

		private void RemoveToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.lbxEntry.SelectedItem != null)
			{
				object selectedItem = this.lbxEntry.SelectedItem;
				if (MessageBox.Show("确定要删除选中的词条？", "盈建科词库", MessageBoxButtons.OKCancel) == DialogResult.OK && DictOperation.RemoveStringEntry(((StringEntry)this.lbxEntry.SelectedItem).XNode.ParentNode, ((StringEntry)this.lbxEntry.SelectedItem).XNode, this.dictFile.FullName))
				{
					this.lbxEntry.Items.Remove(selectedItem);
				}
			}
		}

		private void EditToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.lbxEntry.SelectedItem != null)
			{
				DlgEditEntry.GetInstance(this).ShowDialog(this);
			}
		}

		private void AddEntryToolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdAddEntry();
		}

		private void cmdAddEntry()
		{
			XmlNode xmlNode = (XmlNode)this.tvDirectory.SelectedNode.Tag;
			if (xmlNode.Name.Trim() == "盈建科词库")
			{
				MessageBox.Show("请添加到具体目录", "盈建科提示");
				return;
			}
			if (xmlNode.Attributes["name"].Value == "最近使用的词")
			{
				MessageBox.Show("请添加到具体目录", "盈建科提示");
				return;
			}
			DlgAddEntry.GetInstance(this).ShowDialog(this);
		}

		private void AddSubDirToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DlgAddSubDirectory.GetInstance(this).ShowDialog(this);
		}

		private void RemoveDirToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.tvDirectory.SelectedNode != null && MessageBox.Show("确定要删除选中的目录？", "盈建科词库", MessageBoxButtons.OKCancel) == DialogResult.OK && DictOperation.RemoveDirectory(((XmlNode)this.tvDirectory.SelectedNode.Tag).ParentNode, (XmlNode)this.tvDirectory.SelectedNode.Tag, this.dictFile.FullName))
			{
				DictOperation dictOperation = new DictOperation(this.dictFile.FullName);
				dictOperation.FillCbx(this.comboBox1);
				dictOperation.FillTree(this.tvDirectory);
				this.lbxEntry.Items.Clear();
				XmlNode node = (XmlNode)this.tvDirectory.Nodes[0].Tag;
				this.GetChildAttributes(node, this.lbxEntry);
				this.lbxEntry.Sorted = true;
				this.richTextBox1.Text = "";
			}
		}

		private void EditDirToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (DlgEditDirectory.GetInstance(this).ShowDialog() == DialogResult.OK)
			{
				MessageBox.Show("修改成功", "盈建科词库");
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void AddEntryToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.cmdAddEntry();
		}

		public TextAlignFlags alignFlags;

		public FileInfo dictFile;

		private ExternalCommandData revit;

		private FileInfo tempFile;

		private static DlgDict dlgMain;
	}
}
